package org.yunshang.leather.service.impl;

import org.yunshang.common.core.utils.MapstructUtils;
import org.yunshang.common.core.utils.StringUtils;
import org.yunshang.common.mybatis.core.page.TableDataInfo;
import org.yunshang.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.yunshang.leather.domain.TLeather;
import org.yunshang.leather.domain.bo.TLeatherBo;
import org.yunshang.leather.domain.vo.TLeatherVo;
import org.yunshang.leather.mapper.TLeatherMapper;
import org.yunshang.leather.service.ITLeatherService;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 皮料信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-30
 */
@RequiredArgsConstructor
@Service
public class TLeatherServiceImpl implements ITLeatherService {

    private final TLeatherMapper baseMapper;

    /**
     * 查询皮料信息
     */
    @Override
    public TLeatherVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询皮料信息列表
     */
    @Override
    public TableDataInfo<TLeatherVo> queryPageList(TLeatherBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TLeather> lqw = buildQueryWrapper(bo);
        Page<TLeatherVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询皮料信息列表
     */
    @Override
    public List<TLeatherVo> queryList(TLeatherBo bo) {
        LambdaQueryWrapper<TLeather> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TLeather> buildQueryWrapper(TLeatherBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TLeather> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TLeather::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TLeather::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TLeather::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增皮料信息
     */
    @Override
    public Boolean insertByBo(TLeatherBo bo) {
        TLeather add = MapstructUtils.convert(bo, TLeather.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改皮料信息
     */
    @Override
    public Boolean updateByBo(TLeatherBo bo) {
        TLeather update = MapstructUtils.convert(bo, TLeather.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TLeather entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除皮料信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
